﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Collections;
using System.Web.Security;


namespace TwoWheels.Account
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
        }

        protected void LoginButton_buttonClick(object sender, EventArgs e)
        {
            String user = LoginUser.UserName;
            String password = LoginUser.Password;

            //walidacja
            // wyciaganie z bazy
            const string cnStr = @"Data Source=DUO-P8600;Initial Catalog=TwoWheels;Integrated Security=True";
            TwoWheelsDatabase tdb = new TwoWheelsDatabase(cnStr);
            
            var client = from c in tdb.clients where (c.eMail.Equals(user) && c.HashCode.Equals(password)) select c ;

            if (client.ToList().Count == 0)
            {
                try
                {
                    var admin = (from d in tdb.owners where (d.HashCode.Equals(password) && d.NIP == int.Parse(user)) select d);

                    if (admin.ToList().Count > 0)
                    {
                        Session["AdministratorPanel"] = new AdminPanel();
                    }

                } catch(System.FormatException ex)
                {
                    //print erroru da za duzo informacji potencjalnym wlamywaczom - ze login admina to same cyfry. Stad nic nie piszemy
                }
             
                
            }
            else
            {    

                int ID = (from c in client select c.KlientID ).ToList().First();
                Session["UserShoppingCartInfo"] = new Account.UserShoppingCart(ID);
            }
            

            
        }
    }
}
